using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game500.Client.Src.Logic
{
  internal class ArrowDrawer
  {
    private readonly Texture2D myTexture;

    public ArrowDrawer(ResourceManager resourceManager)
    {
      myTexture = resourceManager.GetTexture("arrow.png");
    }

    public void DrawArrow(Vector3 playerPos, Vector3 enemyPos, Renderer renderer, Color color)
    {
      float xdist = Math.Abs(playerPos.X - enemyPos.X);
      float ydist = Math.Abs(playerPos.Y - enemyPos.Y);

      float width = renderer.Camera.ScreenWidth / 2;
      float height = renderer.Camera.ScreenHeight / 2;


      if (xdist > width || ydist > height) DrawArrowPrivate(playerPos, enemyPos, renderer, color);
    }

    private void DrawArrowPrivate(Vector3 playerPos, Vector3 enemyPos, Renderer renderer, Color color)
    {
      float x = enemyPos.X - playerPos.X;
      float y = playerPos.Y - enemyPos.Y;

      var rotation = (float)Math.Atan(y / x);
      if (x < 0) rotation = (float)(Math.PI + rotation);

      renderer.RenderSprite(myTexture, playerPos, new Vector2(0, 0.5f), Vector2.One, color, rotation);
    }
  }
}